package com.ybg.bookmember.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.ybg.bookcommons.dto.BookinfosDTO;
import com.ybg.bookcommons.dto.ReservationsDTO;
import com.ybg.bookcommons.dto.ReturnDTO;
import com.ybg.bookcommons.vo.BookinfosVO;
import com.ybg.bookmember.services.BookinfosService;
import com.ybg.bookmember.services.ReservationsService;
import lombok.Data;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("member")
public class MemberCtrl {
    /**
     * 用户多条件查询图书 按照书名 按照分类 按照作者 按照出版社 进行模糊查询
     * @param book
     * @param current
     * @param size
     * @return
     */
    @Resource
    private BookinfosService bookinfosService;
    @Resource(name = "stringRedisTemplate")
    private StringRedisTemplate redisTemplate;
    @Resource
    private ReservationsService reservationsService;

    @RequestMapping(value = "/searchbook", method = RequestMethod.GET)
    public Page<BookinfosDTO> search(BookinfosVO book, int current, int size) {
        // 调用服务层方法进行实际查询
        return bookinfosService.selectBooksMultiCondition(book, current, size);
    }

    @RequestMapping(value="/rese/{isbn}",method = RequestMethod.GET)
    public List<ReservationsDTO> rese(@PathVariable String isbn,HttpServletRequest  request){
        String token = request.getHeader("Authorization");
        String user = redisTemplate.opsForValue().get(token);
        Long userid = Long.parseLong(user.split(",")[0]);
        List<ReservationsDTO> rtos = reservationsService.reserv(isbn, userid);
        return rtos;
    }

//取消预约
@RequestMapping(value = "/cancel_rese/{isbn}", method = RequestMethod.GET)
public ReturnDTO cancelRese(@PathVariable String isbn, HttpServletRequest request) {
    String token = request.getHeader("Authorization");
    String user = redisTemplate.opsForValue().get(token);
    Long userid = Long.parseLong(user.split(",")[0]);

    // 调用服务方法执行取消预约逻辑
    boolean result = reservationsService.destoryReserv(isbn, userid);

    ReturnDTO rto;
    if (result) {
        rto = ReturnDTO.builder().status("200").message("取消成功").build();
    } else {
        rto = ReturnDTO.builder().status("500").message("取消失败").build();
    }
    return rto;
}

}
